// -----------------------------------------------------------------------
//  <copyright file="CheckUserNameExistsQuery.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024-05-30 16:05</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Domain.Entity;
using DaprPlus.Identity.Domain.Entities;
using DaprPlus.Identity.Domain.Specs;
using DaprPlus.Specifications;


namespace DaprPlus.Identity.Application.Identity.Users.Queries;

[AuthConfig(typeof(User), ResourceAccessType.Anonymous, "CheckAccountExists")]
public class CheckAccountExistsQuery : IRequest<bool>, IQuery
{
    public string Account { get; set; } = null!;
}

public class CheckUserNameExistsQueryHandler(IRepository<User> userRepository)
    : IRequestHandler<CheckAccountExistsQuery, bool>
{
    public Task<bool> Handle(CheckAccountExistsQuery request, CancellationToken token)
    {
        ISpecification<User> spec = request.Account.IsEmail()
            ? new UserByNormalizedEmailSpec<User>(request.Account.ToUpper())
            : request.Account.IsMobileNumber()
                ? new UserByPhoneNumberSpec<User>(request.Account)
                : new UserByNormalizedNameSpec<User>(request.Account.ToUpper());
        return userRepository.AnyAsync(spec, token);
    }
}

